package com.dong.ch03_stack_queue;

public class MyStack {

    public static class StackArr<E> implements Stack<E> {
        S03_MyArrayEnhance.Array<E> array = null;

        public StackArr() {
            this(8);
        }
        public StackArr(int size) {
            if(size<=0) {
                size=8;
            }
            array = new S03_MyArrayEnhance.Array(size);
        }
        @Override
        public void push(E e) {
            array.addLast(e);
        }

        @Override
        public E pop() {
            return array.deleteLast();
        }

        @Override
        public E peek() {
            return array.get(array.getSize()-1);
        }

        @Override
        public int getSize() {
            return array.getSize();
        }

        @Override
        public boolean isEmpty() {
            return array.isEmpty();
        }
        @Override
        public String toString() {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < getSize()-1; i++) {
                sb.append(array.get(i)).append(" , ");
            }
            sb.append(array.getLast());
            return "StackArr{"+sb+"} top" ;
        }
    }
    private static void test1() {
        StackArr<Integer> stack = new StackArr<>();
        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }
        stack.pop();
        System.out.println(stack);
    }
    private static void test2() {

    }


    public static void main(String[] args) {
       test1();
    }

}
